package com.cxb.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cxb.MainActivity;
import com.cxb.R;
import com.cxb.entity.SettingButtonInfo;
import com.cxb.utils.SharedPreferencesUtils;

public class SettingAdapter extends BaseAdapter {

	private class GridHolder {
		TextView tvLabel;
		ImageView ivIcon;
	}

	private Context context;

	private List<SettingButtonInfo> list;
	private LayoutInflater mInflater;
	private SharedPreferencesUtils spUtils;
	public SettingAdapter(Context c) {
		super();
		this.context = c;
		spUtils=new SharedPreferencesUtils(context);
	}

	public void setList(List<SettingButtonInfo> list) {
		this.list = list;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int index) {

		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(final int index, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.setting_grid_item, null);
			holder = new GridHolder();
			holder.tvLabel = (TextView) convertView.findViewById(R.id.tv_label);
			holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
			convertView.setTag(holder);

		} else {
			holder = (GridHolder) convertView.getTag();

		}
		final SettingButtonInfo sbiInfo = list.get(index);
		if (sbiInfo != null) {

			holder.tvLabel.setText(sbiInfo.getName());

			holder.ivIcon.setImageResource(sbiInfo.getRes());

			// holder.itb.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// // 判断4s号码是否正确
			// if (sbiInfo.getIntent() != null)
			// context.startActivity(sbiInfo.getIntent());
			//
			// }
			// });

 
			if (sbiInfo.getName().equals(context.getString(R.string.setting_upgrade)) && MainActivity.update_find) {// 更新
				convertView.findViewById(R.id.ivRedPoint).setVisibility(
						View.VISIBLE);

			}
			if (sbiInfo.getName().equals(context.getString(R.string.setting_wexin)) && !spUtils.hasBoundRecord()) {// 更新
				convertView.findViewById(R.id.ivRedPoint).setVisibility(
						View.VISIBLE);

			}

		}
		return convertView;
	}
}
